package com.jxinternet.platform.services.service.impl.steps;

import com.jxinternet.platform.services.entity.domian.Fish;
import com.jxinternet.platform.services.entity.domian.Log;
import com.jxinternet.platform.services.entity.domian.Message;
import com.jxinternet.platform.services.entity.domian.User;
import com.jxinternet.platform.services.entity.domian.Wealth;
import com.jxinternet.platform.services.entity.mysql.FishMysql;
import com.jxinternet.platform.services.protocol.BaseProtocol;
import com.jxinternet.platform.services.protocol.Constants;
import com.jxinternet.platform.services.repository.cassandra.FishCassandraRepository;
import com.jxinternet.platform.services.repository.cassandra.UserCassandraRepository;
import com.jxinternet.platform.services.repository.cassandra.WealthCassandraRepository;
import com.jxinternet.platform.services.repository.convert.FishContert;
import com.jxinternet.platform.services.repository.convert.LogConvert;
import com.jxinternet.platform.services.repository.convert.UserConvert;
import com.jxinternet.platform.services.repository.convert.WealthContert;
import com.jxinternet.platform.services.repository.mysql.FishRepository;
import com.jxinternet.platform.services.repository.mysql.LogRepository;
import com.jxinternet.platform.services.repository.mysql.UserRepository;
import com.jxinternet.platform.services.repository.mysql.WealthRepository;
import java.util.Date;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by gold-jx on 2017/7/19.
 */
@Slf4j
@Component
public class UserInfoSteps {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private WealthRepository wealthRepository;

    @Autowired
    private LogRepository logRepository;

    @Autowired
    private FishRepository fishRepository;

    @Autowired
    private UserCassandraRepository userCassandraRepository;

    @Autowired
    private WealthCassandraRepository wealthCassandraRepository;

    @Autowired
    private FishCassandraRepository fishCassandraRepository;

    @Autowired
    private UserConvert userConvert;

    @Autowired
    private WealthContert wealthContert;

    @Autowired
    private LogConvert logConvert;

    @Autowired
    private FishContert fishContert;





    /**
     * step1
     * 验证请求
     */
    public Message step1_validateRequest(String userName) {
        Message message = new Message();
        try {
            //1.用户名不能为空
            if ("".equals(userName) || userName == null) {

                message.setCode(BaseProtocol.UserInfo_Init_Invalid_UserName_NotNull_Code);
                message.setRes(BaseProtocol.UserInfo_Init_UserName_NotNull_Des);
                //log.error(message.getRes(), message.getCode());
                return message;
            }
        } catch (Exception e) {
            message.setCode(BaseProtocol.UserInfo_Init_Step01_Exception_Code);
            message.setRes(BaseProtocol.UserInfo_Init_Step01_Exception_Res);
            //log.error(message.getRes(), message.getCode());
            return message;
        }
        return message;

    }

    /**
     * step2
     * 获取用户
     */
    public Message step2_getUser(String userName) {
        Message message = new Message();
        try {
            User user = userConvert.convertMysqlDomainToModel(userRepository.findByUserName(userName));

            if (user == null) {
                message.setCode(BaseProtocol.User_Invalid_User_Not_isExist_Code);
                message.setRes(BaseProtocol.User_Invalid_User_Not_isExist_Res);
                //log.error(message.getRes(), message.getCode());
                return message;
            }
            message.setData(user);
            return message;
        } catch (Exception e) {
            message.setCode(BaseProtocol.UserInfo_Init_Step02_Exception_Code);
            message.setRes(BaseProtocol.UserInfo_Init_Step02_Exception_Res);
            //log.error(message.getRes(), message.getCode());
            return message;
        }
    }


    /**
     * step3
     * 获取财富
     */
    public Message step3_getWealth(String userName) {
        Message message = new Message();
        try {
            Wealth wealth =wealthContert.convertMysqlDomainToModel(wealthRepository.findByUserName(userName));
            if (wealth == null) {
                message.setCode(BaseProtocol.UserInfo_Init_Invalid_Wealth_Init_Code);
                message.setRes(BaseProtocol.UserInfo_Init_Invalid_Wealth_Init_Res);
                //log.error(message.getRes(), message.getCode());
                return message;
            }
            message.setData(wealth);
            return message;

        } catch (Exception e) {
            message.setCode(BaseProtocol.UserInfo_Init_Step03_Exception_Code);
            message.setRes(BaseProtocol.UserInfo_Init_Step03_Exception_Res);
            //log.error(message.getRes(), message.getCode());
            return message;
        }
    }

    /**
     * step4
     * 获取鱼类
     */
    public Message step4_getFish() {
        Message message = new Message();
        try {
            fishRepository.findAll();
            List<Fish> fishList =fishContert.convertMysqlDomainToModel((List<FishMysql>)fishRepository.findAll());
            if (fishList == null) {
                message.setCode(BaseProtocol.UserInfo_Init_Invalid_Fish_Init_Code);
                message.setRes(BaseProtocol.UserInfo_Init_Invalid_Fish_Init_Res);
                //log.error(message.getRes(), message.getCode());
                return message;
            }
            message.setData(fishList);
            return message;
        } catch (Exception e) {
            message.setCode(BaseProtocol.UserInfo_Init_Step04_Exception_Code);
            message.setRes(BaseProtocol.UserInfo_Init_Step04_Exception_Res);
            //log.error(message.getRes(), message.getCode());
            return message;
        }

    }


    /**
     * step5
     * 加载缓存
     */
    public Message step5_cache(User user, Wealth wealth, List<Fish> fishList) {
        Message message = new Message();
        try {
            //测试
            //User user1=userRepository.findByUserName("hanmeimei");
           // User user1=userRepos.findByUserName("hanmeimei");
            //System.out.println(user1);
            //1.添加用户缓存
            user.setCreateTime(null);
            user.setUpdateTime(null);
            userCassandraRepository.save(userConvert.convertCassandra(user));
            //2.添加财富缓存
            wealthCassandraRepository.save(wealthContert.convertCassandra(wealth));
            //3.添加鱼
            fishCassandraRepository.save(fishContert.convertCassandra(fishList));
            return message;

        } catch (Exception e) {
            e.printStackTrace();
            message.setCode(BaseProtocol.UserInfo_Init_Step05_Exception_Code);
            message.setRes(BaseProtocol.UserInfo_Init_Step05_Exception_Res);
            //log.error(message.getRes(), message.getCode());
            return message;
        }


    }

    public Message step6_saveLog(String userName, String loginIp) {
        Message message = new Message();
        try {
            Log log = new Log();
            log.setUserName(userName);
            log.setLoginIp(loginIp);
            log.setType(BaseProtocol.Area_User_LoadWealth);
            log.setOperation("加载财富");
            log.setState(Constants.STATE_NORMAL);
            log.setPlatformNo(Constants.PLATFORM_NO);
            log.setCreateTime(new Date());
            log.setUpdateTime(new Date());
            log.setVersion(Constants.VERSION_INIT);
            logRepository.save(logConvert.convertMysql(log));
            return message;
        } catch (Exception e) {
            message.setCode(BaseProtocol.UserInfo_Init_Step06_Exception_Code);
            message.setRes(BaseProtocol.UserInfo_Init_Step06_Exception_Res);
            //log.error(message.getRes(), message.getCode());
            return message;
        }
    }

}
